package com.quanyan.user.web;

import com.quanyan.api.APIResponse;
import com.quanyan.user.model.vo.request.ReqAppBannerInfo;
import com.quanyan.user.model.vo.response.RespAppBanner;
import com.quanyan.user.service.UserAppBannerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by slong.guo on 2016/12/13.
 */
@RestController
@RequestMapping(value = "/api/user/banner", method = RequestMethod.POST)
public class UserAppBannerController {
    private static Logger logger = LoggerFactory.getLogger(UserAppBannerController.class);

    @Autowired
    private UserAppBannerService userAppBannerService;


    @RequestMapping("getBannerInfo")
    public APIResponse<List<RespAppBanner>> getBannerInfo(@RequestBody Map map, HttpServletRequest request) {

        if (!map.containsKey("type")) {
            return APIResponse.returnFail("请输入有效参数");
        }
//            if (!map.containsKey("city")) {
//                return APIResponse.returnFail("请输入有效参数");
//            }
        int city = 0;
        int type = (int) map.get("type");
        if (map.containsKey("city")) {
            city = (int) map.get("city");
        }

        Integer sportType = (Integer)map.get("sportType");
        Integer competition = (Integer)map.get("competition");
        sportType = sportType == null ? -1 : sportType;
        competition = competition == null ? -1 : competition;

        Object pt = map.get("platformType");
        Integer platformType = null;
        if (pt instanceof Integer) {
            platformType = (Integer) pt;
        } else if (pt instanceof String) {
            try {
                platformType = Integer.valueOf((String) pt);
            } catch (Exception e) {
                platformType = null;
            }
        }

        String ver = (String) map.get("ver");
        Integer uid = (Integer) request.getAttribute("uid");
        int actualUID = uid != null && uid > 0 ? uid : 0;

        Object appId = map.get("larkAppId");
        Integer larkAppId = 0;

        if (appId instanceof Integer) {
            larkAppId = (Integer) appId;
        } else if (appId instanceof String) {
            try {
                larkAppId = Integer.valueOf((String) appId);
            } catch (Exception e) {
                larkAppId = 0;
            }
        }

        if (larkAppId == null || larkAppId.compareTo(0) <= 0) {
            return userAppBannerService.getBannerInfo(actualUID, type, city, sportType, competition, platformType, ver);
        } else {
            ReqAppBannerInfo reqBanner = new ReqAppBannerInfo();
            reqBanner.setType(type);
            reqBanner.setCity(city);
            reqBanner.setSportType(sportType);
            reqBanner.setCompetition(competition);
            reqBanner.setLarkAppId(larkAppId);
            return userAppBannerService.getBannerInfoNew(actualUID, reqBanner);
        }


    }
}
